  function chexist(names,opath); 
% function chexist(names,opath); 
% Checks if 
% a) Directory opath exists 
% b) Files with the names in input names exists in opath 
% 
% if names == [], only the existance of the path is checked 
% 
% Returns error message otherwise 
% Will return to the root path from where run 
%
% names is a column cell array with the string names of the 
%       objects to check if they exist 
% opath path where the objects should be 
% 
% Alejandro Justiniano 3/25/04 
% Modified 12/31/2004 
% 11/2/2005 if opath [] use current directory 
% ================================================
cuc=cd;
if nargin == 1 | isempty( opath ) == 1
    opath = cuc;
else
    che=exist(opath,'file');
    if che == 0
        disp( ['Directory ',opath,' does not exist']);
        error(['Directory ',opath,' does not exist']);
    end
    clear che
end

if isempty( names ) == 1 
    return 
end 

flag_str = isstr( names );
if flag_str == 0
    names=names(:);
else
    if min( size( names )  ) > 1
        error('In CHEXIST if NAMES is a string, only one allowed')
    end
    names = cellstr( names ); 
end

cd(opath); 
pp=1; 
for pp=1:size(names,1); 
    chs=exist( names{pp,:} ,'file' ); 
    if chs == 0 
        cd(cuc); 
        disp(['In directory ',opath] ); 
        disp(['File  ',names{pp,:},'  does not exist'] );
        error(['In directory ',opath,' file ',names{pp,:}, ' does not exist']); 
    end; 
end; 
cd(cuc); 